package com.uiotsoft.small.host.dataSync;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ProduceMsg {
    private String sn;
    private String appkey;
    private int start;
    private int end;
    private String userUnique;
    private String msgId;

    public void setStart(int start) {
        this.start = start;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public ProduceMsg(String sn, String appkey){
        this.sn = sn;
        this.appkey = appkey;
    }

    public String chooseSendMsg(String topic) {
        return chooseSendMsg(topic,0);
    }

    public String chooseSendMsg(String topic,int ran) {
        int random;
        if (ran == 0){
            random = (int) (Math.random() * (end-start) + start);
        }else {
            random = ran;
        }
        JSONObject msg = null;
        switch (topic) {
                case "listDeviceSync":
                msg = getSendMsg("deviceList", getDeviceList(random));
                break;
            case "synAllSmartList":
                msg = getSendMsg("smartList", getSmartList(random));
                break;
            case "syncUserRole":
                msg = getSendMsg("roleList", getUserRoleList(random));
                break;
            case "syncRoom":
                msg = getSendMsg("roomList", getRoomList(random));
                break;
            case "syncFloor":
                msg = getSendMsg("floorList", getFloorList(random));
                break;
            case "syncRole":
                msg = getSendMsg("roleList", getRoleList(random));
                break;
            case "syncHomeUser":
                msg = getSendMsg("userList", getHomeUserList());
                break;
            case "listRoomDeviceSort":
                msg = getSendMsg("deviceList", getRoomDeviceSortList(random));
                break;
            case "0/listUserFrequency":
                JSONObject temp0 = getSendMsg("optList", getDeviceSmartOpt(0,random));
                msg = handleMsg(temp0,"0");
                break;
            case "1/listUserFrequency":
                JSONObject temp1 = getSendMsg("optList", getDeviceSmartOpt(1,random));
                msg = handleMsg(temp1,"1");
                break;
            case "0/syncIndexDeviceSmart":
                msg = getSendMsg("indexDataList", getIndexDeviceList(0,random));
                break;
            case "1/syncIndexDeviceSmart":
                msg = getSendMsg("indexDataList", getIndexDeviceList(1,random));
                break;
            default:
                break;
        }
        return msg.toJSONString();
    }

    public JSONObject handleMsg(JSONObject jsonObject,String type){
        JSONObject paramsJson = new JSONObject();
        JSONObject headerJson = jsonObject.getJSONObject("header");
        paramsJson.put("dataType",type);
        headerJson.put("params",paramsJson);
        jsonObject.put("header",headerJson);
        return jsonObject;
    }

    public JSONObject getSendMsg(String name, JSONArray list) {
        try {
            String header = "{\"params\":{},\"version\":\"1.0\",\"isEncrypt\":\"true\",\"compression\":\"none\",\"clientType\":\"host\"}";
            JSONObject headerJson = JSONObject.parseObject(header);
            headerJson.put("identity", sn);
            headerJson.put("appkey", appkey);
            headerJson.put("msgId",msgId);
            headerJson.put("userUnique",sn.substring(26));
            JSONObject payloadJson = new JSONObject();
            JSONObject dataJson = new JSONObject();
            dataJson.put(name, list);
            dataJson.put("sn",sn);
            payloadJson.put("data", dataJson);
            payloadJson.put("code", 0);
            payloadJson.put("desc", "");
            JSONObject msgJson = new JSONObject();
            msgJson.put("payload", payloadJson);
            msgJson.put("header", headerJson);
            return msgJson;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getDeviceList(int random) {
        try {
            String device = "{\"virtualType\":-1,\"deviceId\":1,\"deviceName\":\"test\",\"roomId\":1,\"deviceSubMac\":\"10003665\",\"buttonLocation\":\"\",\"deriveDeviceId\":-1,\"deviceMac\":\"1000366510003665\",\"hardwareVersion\":\"V3.30\",\"deviceType\":\"intelligentLighting\",\"deviceUuid\":\"\",\"zigBeeSoftwareVersion\":\"V5.40\",\"deviceOnlineState\":0,\"repeaterId\":-1,\"signalStrength\":28,\"channelNum\":0,\"mainDeviceFlag\":1,\"mainType\":2,\"electricQuantity\":100,\"controlType\":1,\"deviceSeries\":0,\"zigBeeHardwareVersion\":\"\",\"subType\":2,\"deviceModel\":\"l_zf_double_switch\",\"softwareVersion\":\"V3.61\",\"powerType\":\"ac\"}";
            JSONArray deviceList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject deviceJson = JSONObject.parseObject(device);
                deviceJson.put("deviceId", i + 1);
                deviceJson.put("deviceName", "testDevice" + (i + 1));
                deviceList.add(deviceJson);
            }
            return deviceList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getRoomDeviceSortList(int random) {
        try {
            String device = "{ \"roomId\": 1, \"userUnique\": \"testSN\", \"deviceId\": 8, \"orderNum\": 2 }";
            JSONArray deviceList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject deviceJson = JSONObject.parseObject(device);
                deviceJson.put("deviceId", i + 1);
                deviceJson.put("orderNum", i + 1);
                deviceJson.put("userUnique", "testUserUnique"+(i + 1));
                deviceList.add(deviceJson);
            }
            return deviceList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getDeviceSmartOpt(int type, int random) {
        try {
            JSONArray deviceSmartList = new JSONArray();
            if (type == 0){
                for (int i = 0; i < random; i++) {
                    JSONObject deviceSmartJson = new JSONObject();
                    deviceSmartJson.put("smartId", i + 1);
                    deviceSmartJson.put("optCount", i + 1);
                    deviceSmartJson.put("userUnique", "testUserUnique"+(i + 1));
                    deviceSmartList.add(deviceSmartJson);
                }
            }else {
                for (int i = 0; i < random; i++) {
                    JSONObject deviceSmartJson = new JSONObject();
                    deviceSmartJson.put("deviceId", i + 1);
                    deviceSmartJson.put("optCount", i + 1);
                    deviceSmartJson.put("userUnique", "testUserUnique"+(i + 1));
                    deviceSmartList.add(deviceSmartJson);
                }
            }

            return deviceSmartList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getSmartList(int random) {
        try {
            String smart = "{\"smartId\":2,\"smartName\":\"智能10-1\",\"smartIcon\":\"smart_icon_get_up\",\"roomId\":1,\"runType\":1,\"isManualExec\":1,\"execSwitch\":1,\"createSource\":1,\"isRepeat\":1,\"repeatData\":\"0,1,2,3,4,5,6\",\"startTime\":\"00:00\",\"endTime\":\"23:59\",\"createTime\":\"2022-04-11 08:50:27\",\"newlyCreated\":0}";
            JSONArray deviceList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject smartJson = JSONObject.parseObject(smart);
                smartJson.put("smartId", i + 1);
                smartJson.put("smartName", "testSmart" + (i + 1));
                deviceList.add(smartJson);
            }
            return deviceList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getRoomList(int random) {
        try {
            String obj = "{ \"roomId\": 1, \"floorId\": 1, \"orderNum\": 10, \"roomIconType\": 0, \"roomName\": \"客厅\", \"roomIcon\": \"room_living_1_bg\", \"roomCategoryType\": \"roomCategory.livingRoom\"}";
            JSONArray objList = new JSONArray();

            for (int i = 0; i < random; i++) {
                JSONObject objJson = JSONObject.parseObject(obj);
                objJson.put("roomId", i + 1);
                objJson.put("roomName", "testRoom" + (i + 1));
                objList.add(objJson);
            }
            return objList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getFloorList(int random) {
        try {
            String obj = "{ \"floorId\": 1, \"floorName\": \"一楼\", \"orderNum\": 10 }";
            JSONArray objList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject objJson = JSONObject.parseObject(obj);
                objJson.put("floorId", i + 1);
                objJson.put("floorName", "testFloor" + (i + 1));
                objList.add(objJson);
            }
            return objList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getRoleList(int random) {
        try {
            String obj = "{ \"roleId\": 1, \"roleName\": \"超级管理员\", \"roleType\": \"newRole\", \"deviceEditSwitch\": 1, \"smartEditSwitch\": 1 }";
            JSONArray objList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject objJson = JSONObject.parseObject(obj);
                objJson.put("roleId", i + 4);
                objJson.put("roleName", "testRole" + (i + 1));
                objList.add(objJson);
            }
            return objList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getUserRoleList(int random) {
        try {
            String obj = "{\"userRoleId\":1,\"roleId\":2,\"userUnique\":\"76e464\"}";
            JSONArray objList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject objJson = JSONObject.parseObject(obj);
                objJson.put("userUnique", "testUserUnique"+(i + 1));
                objJson.put("userRoleId", (i + 10000));
                objList.add(objJson);
            }
            return objList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getHomeUserList() {
        try {
            String obj = "{\"userName\":\"15286800731\",\"nickName\":\"刘阳\",\"userUnique\":\"9bfec69bf8ae179ece5cf6775468ee6a\",\"createTime\":\"2021-01-25 17:48:26\",\"userType\":1,\"applyMode\":0}";
            JSONArray objList = new JSONArray();
            JSONObject objJson = JSONObject.parseObject(obj);
            objList.add(objJson);
            return objList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONArray getIndexDeviceList(int type, int random){
        try {
            String obj = "{\"deviceSmartId\":1,\"orderType\":0,\"dataType\":1,\"orderNum\":1,\"userUnique\":\"\"}";
            JSONArray objList = new JSONArray();
            for (int i = 0; i < random; i++) {
                JSONObject objJson = JSONObject.parseObject(obj);
                objJson.put("userUnique", userUnique);
                objJson.put("deviceSmartId", (type + i));
                objJson.put("orderNum", (i + 1));
                objJson.put("dataType", type);
                objList.add(objJson);
            }
            return objList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public void setUserUnique(String userUnique) {
        this.userUnique = userUnique;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }
}
